package com.projectx.action.chat.DevoteActivity;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;

import java.util.ArrayList;
import java.util.List;

public class AddDevoteAdapter extends RecyclerView.Adapter<AddDevoteAdapter.VH> {

    private Context context;
    private static List<String> count = new ArrayList<>();
    public AddDevoteAdapter(Context context) {
        this.context = context;
    }
    protected static List<String> data=new ArrayList<>();
    private List <String> data_=new ArrayList<>();
    static{
        count.add("");
        count.add("");
        count.add("");
    }

    @NonNull
    @Override
    public AddDevoteAdapter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adddevote_item, parent, false);
        return new AddDevoteAdapter.VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final AddDevoteAdapter.VH holder, final int position) {
        holder.editText.setHint("选项" + (holder.getAdapterPosition() + 1));
        // holder.imageView.setImageBitmap(bitmaps.get(position));
        holder.editText.setText("");
        if (holder.getLayoutPosition() == 0 || holder.getLayoutPosition() == 1) {
            holder.imageView.setImageResource(R.drawable.ic_adjust_black_24dp);
        } else if (holder.getAdapterPosition() == getItemCount()-1) {
            holder.editText.setHint("添加选项");
            holder.editText.setEnabled(false);
            holder.editText.setBackground(null);
            holder.imageView.setImageResource(R.drawable.ic_add_circle_black_24dp);
            holder.click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // 点击事件
                    // addData(position);
                    count.add("");
                    notifyItemInserted(holder.getLayoutPosition());
                    notifyItemChanged(holder.getLayoutPosition());
                    Log.d("AddDevoteActivity", "自闭" + position);
                }
            });
            Log.d("AddDevoteActivity", getItemCount() + "自闭呀呀");
        }
        else if (holder.getLayoutPosition() >= 2) {
            holder.imageView.setImageResource(R.drawable.ic_remove_circle_black_24dp);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //removeData(position);
                    Toast.makeText(v.getContext(),"删除选项会清空其他选项的内容哦，气不气?",Toast.LENGTH_SHORT).show();
                    holder.editText.setText("");
                    count.remove(position);
                    notifyItemRemoved(holder.getAdapterPosition());
                    notifyItemRangeChanged(holder.getAdapterPosition(), getItemCount());
                    // notifyDataSetChanged();

                    Log.d("AddDevoteActivity", "自闭" + position);
                    Log.d("AddDevoteActivity",getItemCount()+"真的自闭");
                }
            });
        }
        data=data_;
    }
    public void addData(int position) {
        count.add("");
        //添加动画
        notifyItemInserted(position);
        notifyItemChanged(position);
        // notifyItemRangeChanged(0, count.size());
        //notifyDataSetChanged();
    }

    public void removeData(int position) {
        try{
            count.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position-1, count.size() - position);
            // notifyDataSetChanged();
        }catch (IndexOutOfBoundsException e){
            count.remove(position-1);
            notifyItemRemoved(position-1);
            notifyItemRangeChanged(position-1, count.size() - position+1);
            //  notifyDataSetChanged();
        }

        // notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return count.size();
    }

    public static class VH extends RecyclerView.ViewHolder {

        EditText editText;
        ImageView imageView;
        View click;

        public VH(@NonNull View itemView) {
            super(itemView);
            editText = itemView.findViewById(R.id.edittext);
            imageView = itemView.findViewById(R.id.imageView);
            click = itemView.findViewById(R.id.click);
        }
    }
}

